package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.config.MinioProperties;
import com.atguigu.spzx.manager.service.FileService;
import com.atguigu.spzx.model.exception.SpzxException;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Override
    public String upload(MultipartFile file, String module) {
        //0、获取上传文件的名称拼接：上传路径(module+时间目录+唯一文件名)
        if(file.isEmpty()){
            throw new SpzxException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }
        try {
            String filename = file.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf("."));
            //  avatar/2024-03/13/1124234234_21321ab21.jpg
            filename = module+new DateTime().toString("/yyyy-MM/dd/")+ System.currentTimeMillis()+"_"
                    + UUID.randomUUID().toString().replace("-","").substring(0,8)+suffix;

            //1、创建minio客户端对象
            MinioClient client = MinioClient.builder()
                    .endpoint(MinioProperties.ENDPOINT) //minio服务器地址
                    .credentials(MinioProperties.ACCESS_KEY, MinioProperties.SECRET_KEY) //minio账号密码
                    .build();
            //2、上传文件
            client.putObject(PutObjectArgs.builder()
                    .bucket(MinioProperties.BUCKET) //minio中存储文件的桶
                    .stream(file.getInputStream(),file.getSize() , -1)//上传文件流
                    .object(filename) //桶内存储上传的文件的路径和文件名称
                    .build()
            );
            //3、拼接文件访问路径返回
            String path = MinioProperties.ENDPOINT+"/"+MinioProperties.BUCKET+"/"+filename;
            return path;
        } catch (Exception e) {
            throw new SpzxException( ResultCodeEnum.FILE_UPLOAD_ERROR,e);
        }

    }
    //http://192.168.1.170:9000/spzx/avatar/2024-03/13/1710295020809_74ae5468.jpg
    @Override
    public void deleteFile(String path) {
        try {
            //1、创建minio客户端对象
            MinioClient client = MinioClient.builder()
                    .endpoint(MinioProperties.ENDPOINT) //minio服务器地址
                    .credentials(MinioProperties.ACCESS_KEY, MinioProperties.SECRET_KEY) //minio账号密码
                    .build();
            //2、删除
            path = path.substring((MinioProperties.ENDPOINT+"/"+MinioProperties.BUCKET+"/").length());
            client.removeObject(RemoveObjectArgs.builder()
                    .bucket(MinioProperties.BUCKET)
                    .object(path)
                    .build());
        } catch (Exception e) {
            throw new SpzxException(ResultCodeEnum.FILE_DELETE_ERROR,e);
        }
    }
}
